package cn.bocweb.spinning.view.forun;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.ForumDetailBean;
import cn.bocweb.spinning.model.Bean.UserBean;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.activity.ImgDetailActivity;
import cn.bocweb.spinning.view.common.GlideCircle;
import cn.bocweb.spinning.view.common.MyAdapter;

/**
 * Created by chenchangxing on 16/3/15.
 */
public class ForunDetailAdapter extends MyAdapter<ForumDetailBean.CommentBean> {

    public ForunDetailAdapter(Context context, List<ForumDetailBean.CommentBean> list) {
        super(context, list);
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_forun_detail, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        UserBean.DataEntity dataEntity = new UserBean.DataEntity();
        dataEntity.readInfoFromLocal(parent.getContext());

        if (!TextUtils.isEmpty(list.get(position).getReceiverId()) && list.get(position).getReceiverId().equals(dataEntity.getUserid())) {
            holder.forunDetailReceiver.setVisibility(View.VISIBLE);
            holder.forunDetailReceiver.setText(new StringBuffer("-->  ").append(list.get(position).getReceiver()));
        } else {
            holder.forunDetailReceiver.setVisibility(View.GONE);
        }

        holder.textviewLocation.setText(list.get(position).getLocation());
        int childNum = 0;
        Glide
                .with(parent.getContext())
                .load(list.get(position).getUthumb())
                .placeholder(R.mipmap.login_head)
                .transform(new GlideCircle(parent.getContext()))
                .into(holder.forunDetailHead);

        holder.forunDetailContent.setText(list.get(position).getContent());
        holder.forunDetailName.setText(list.get(position).getNickname());
        holder.forunDetailTime.setText(
                DeviceUtil.getTimeByTwoTimeStampForPhp(
                        Long.parseLong(list.get(position).getTimeline()) * 1000,
                        System.currentTimeMillis()));

        if (list.get(position).getPhoto() == null || list.get(position).getPhoto().isEmpty()) {
            holder.forun_detail_pic.setVisibility(View.GONE);
        } else {
            for (int i = 0; i < holder.forun_detail_pic.getChildCount(); i++) {
                holder.forun_detail_pic.getChildAt(i).setVisibility(View.INVISIBLE);
            }
            holder.forun_detail_pic.setVisibility(View.VISIBLE);
            for (int i = 0; i < list.get(position).getPhoto().size(); i++) {
                holder.forun_detail_pic.getChildAt(childNum).setVisibility(View.VISIBLE);
                Glide
                        .with(context)
                        .load(list.get(position).getPhoto().get(i))
                        .placeholder(R.mipmap.banner_empty)
                        .into((ImageView) holder.forun_detail_pic.getChildAt(childNum));
                holder.forun_detail_pic.getChildAt(childNum).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(parent.getContext(), ImgDetailActivity.class);
                        intent.putStringArrayListExtra(Constant.IMG_DETAIL_URL, (ArrayList<String>) list.get(position).getPhoto());
                        parent.getContext().startActivity(intent);
                    }
                });
                childNum += 2;
            }
        }

        
        return convertView;
    }

    static class ViewHolder {
        @Bind(R.id.forun_detail_head)
        ImageView forunDetailHead;
        @Bind(R.id.forun_detail_name)
        TextView forunDetailName;
        @Bind(R.id.forun_detail_content)
        TextView forunDetailContent;
        @Bind(R.id.forun_detail_reply)
        TextView forunDetailReply;
        @Bind(R.id.forun_detail_time)
        TextView forunDetailTime;
        @Bind(R.id.forun_detail_receiver)
        TextView forunDetailReceiver;
        @Bind(R.id.textview_location)
        TextView textviewLocation;
        @Bind(R.id.forun_detail_pic)
        LinearLayout forun_detail_pic;


        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
